package com.example.demo.demos.web.test;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@RequestMapping("/hello")
@RestController
public class Hello {
    @RequestMapping(value = "/saiHi",method = RequestMethod.GET)
    public String saiHi(){
        return "hello";
    }

    @RequestMapping("/sayHello")
    public String saiHello(){
        return "sayhello";
    }

    @RequestMapping("/m1")
    public String m1(String name){
        return "name="+name;
    }

    @RequestMapping("/m2")
    public String m2(String name,Integer age){
        return "name="+name+",age="+age;
    }

    @RequestMapping("/m3")
    public String m3(Person person){
        return person.toString();
    }

    @RequestMapping("/m4")
    public String m4(@RequestParam(value = "name",required = false)String username){
        return "name="+username+","+username.length();
    }

    @RequestMapping("/m5")
    public String m5(String[] name){
        return Arrays.toString(name)+","+name.length;
    }

    @RequestMapping("/m6")
    public String m6(@RequestParam List<String> name){
        return ""+name+","+name.size();
    }

    @RequestMapping("/m7")
    public String m7(@RequestBody Person person){
        return person.toString();
    }

    @RequestMapping("/m9/{id}")
    public String m9(@PathVariable Integer id){
        return "id="+id;
    }

    @RequestMapping("/m10")
    public void m10(@RequestPart MultipartFile file){
        try {
            file.transferTo(new File("d:/test/"+file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/m11")
    public void m11(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
    }

    @RequestMapping("/m12")
    public String m12(@CookieValue String test,@CookieValue String hhh){
        return test+hhh;
    }

    @RequestMapping("/m13")
    public String m13(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session!=null){
            return (String) session.getAttribute("name");
        }
        return "session为空";
    }

    @RequestMapping("/m14")
    public String m14(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.setAttribute("name","zzzyh");
        return "true";
    }

    @RequestMapping("/m15")
    public String m15(@SessionAttribute String name){
        return name;
    }

    @RequestMapping("/m16")
    public String m16(HttpSession session){
        return (String) session.getAttribute("name");
    }

    @RequestMapping("/m17")
    public String m17(HttpServletRequest request){
        return request.getHeader("user-agent");
    }

    @RequestMapping("/m18")
    public String m17(@RequestHeader("user-agent") String aaaa){
        return aaaa;
    }
}
